<template>
  <div class="content">
    <div>
      <el-form label-position="top" label-width="80px" :model="genForm">
        <el-row :gutter="10">
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-form-item label-position="left" label="实体(业务)名">
                <el-input v-model="genForm.bussinessName"></el-input>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <el-form-item label-position="left" label="操作">
                <el-button type="primary" @click="onGen" size="small">生成</el-button>
                <el-button type="primary" @click="onGenDown" size="small">下载</el-button>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <el-row :gutter="0" v-if="showGenCode">
      <el-col :span="3">
        <div class="grid-content bg-purple">
          <el-tree :data="files" @node-click="handleNodeClick"></el-tree>
        </div>
      </el-col>
      <el-col :span="21" class="code-text">
        <div class="copy" ><el-button size="mini" @click="onCopy">复制</el-button></div>
        <div class="grid-content bg-purple">
          <pre>{{codeText ? codeText : ""}}</pre>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import api from '@/api'
// import selectDict from '@/views/public/selectDict.vue'
// import constant from '@/libs/util.constant'

export default {
  name: 'gen-code',
  components: {
  },
  props: {
    id: {
      type: Number
    }
  },
  data () {
    return {
      showGenCode: false,
      genForm: {
        bussinessName: ''
      },
      codeMap: null,
      codeText: '',
      files: [
        {
          label: '列表页面',
          codeType: 'list'
        }, {
          label: '新增页面',
          codeType: 'insert'
        }, {
          label: '修改页面',
          codeType: 'update'
        }, {
          label: 'Api',
          codeType: 'api'
        }
      ]
    }
  },
  mounted () {
  },
  methods: {
    handleNodeClick (ss) {
      this.codeText = this.codeMap[ss.codeType]
    },
    onGen () {
      var data = this.genForm
      data.entityId = this.id
      data.codeTypes = ['list']
      api.genCode.vue(data).then(res => {
        this.codeMap = res
        this.codeText = this.codeMap[data.codeTypes[0]]
        this.showGenCode = true
      })
    },
    onGenDown () {

    },
    onCopy () {
      var transfer = document.createElement('textarea')
      document.body.appendChild(transfer)
      transfer.value = this.codeText
      transfer.focus()
      transfer.select()
      if (document.execCommand('copy')) {
        document.execCommand('copy')
      }
      transfer.blur()
      this.$message({ type: 'info', message: '已经复制' })
      document.body.removeChild(transfer)
    }
  }

}
</script>

<style lang="scss" scope>
  pre {
    padding:0 20px;
    white-space:pre-wrap;
    word-wrap:break-word;
    letter-spacing:0;
    font:14px/26px 'courier new';
    position:relative;
  }
  .code-text {
    border-radius: 3px;
    background-color:#ecf5ff;
    min-height: 600px
  }
  .copy {
    text-align: right;
    padding: 3px 10px 0 0;
    color: #9999AA;
    cursor: pointer;
  }
</style>
